﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using FormControllers;
using AgencyEntityLib.BaseEntity;
using Ecopsy.Tools;
using SqlLibrary;
using Ecopsy.DataTier;

public partial class Ctl_ctlSurfaceList : System.Web.UI.UserControl
{
    /// <summary>
    /// Визуальное представление
    /// </summary>
    public class grdSurface : SideListWithContracts.SurfaceItemBox
    {
        private SideListWithContracts.SurfaceItemBox _Source = null;
        public grdSurface(SideListWithContracts.SurfaceItemBox Source)
        {
            _Source = Source;
        }

        public string Litera { get { return _Source.Side.Litera; } }
        public string GID { get { return _Source.Constraction.GID + _Source.Side.Litera + _Source.Number.ToString(); } }
        public string ConstractionTypeName { get { return _Source.Constraction.GetTxtConstractionType (); } }
        public string FullAdress { get { return _Source.Constraction.GetTxtAdressProgramm() + " " + _Source.Constraction.GetTxtStreets(); } }
        public int CurrentPrice { get { return _Source.Side.CurrentPrice; } }
        public bool Light { get { return _Source.Constraction.bLight ; } }
        
        ///// <summary>
        ///// Расписание поверхности 
        ///// </summary>
        //public ContractSurfaceItemList SurfaceShedule { get { return _Source.SurfaceShedule; } }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        OnInit();
    }


    protected void OnInit()
    {
        ctlFilterSave1.OkOnClick = ctlFilterSave1_OkOnClick;
        ctlFilterSave1.FilterType = Filter.FilterType.SurfaceList;
        ctlFilterSave1.SaveOnClick = ctlFilterSave1_SaveOnClick;
        ctlFilterSave1.AddKey = String.Empty;

        ctlFilterBar1.OnFindClik = ctlFilterBar1_OnFindClik;
        ctlFilterBar1.OnFilterListClik = ctlFilterBar1_OnFilterListClik;
        ctlFilterBar1.HintServiceMethod = "GetSurfaceListParams";

        ctlFilterList1.OnApp_Click = ctlFilterList1_OnApp_Click;
        ctlFilterList1.FilterType = Filter.FilterType.SurfaceList ;
        ctlFilterList1.AddKey = String.Empty;

        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterList.DataBind();
    }


    private bool ctlFilterList1_OnApp_Click(ListWhere _Where)
    {
        Where = _Where;

        //ctlFilterSave1.DataBind();
        //ctlFilterBar1.DataBind();
        //lvClients.DataBind();
        MyDataBind ();
        return true;
    }

    private bool ctlFilterBar1_OnFilterListClik()
    {
        ctlFilterList1.MyDataBind();
        ModalPopupext_FilterList.Show();
        return true;
    }

    private bool ctlFilterSave1_OkOnClick()
    {
        ModalPopupext_FilterSave.DataBind();
        ModalPopupext_FilterSave.Hide();
        return true;
    }

    private ListWhere ctlFilterSave1_SaveOnClick()
    {
        return (ListWhere)Where.Clone();
    }


    private bool ctlFilterBar1_OnFindClik(string sFindStr)
    {
        Where.ListLoadStatment.Lexeme = ctlFilterBar1.Lexeme;
        MyDataBind();
        return true;
    }


    #region SavePanel

    protected string GetTargetControlID_FilterSave()
    {
        return ctlFilterBar1.BtnAddFilter_ClientID;
    }

    public string GetOkControlID_FilterSave()
    {

        return ctlFilterSave1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterSave()
    {

        return ctlFilterSave1.BtnCancel_ClientID;
    }

    #endregion

    #region ListPanel

    protected string GetTargetControlID_FilterList()
    {
        return ctlFilterBar1.BtnFilterList_ClientID;
    }

    public string GetOkControlID_FilterList()
    {

        return ctlFilterList1.btnSave_ClientID;
    }

    public string GetCancelControlID_FilterList()
    {

        return ctlFilterList1.BtnCancel_ClientID;
    }

    #endregion

    

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        MyDataBind();
    }

    public string KeyPref = "Ctl_ctlSurfaceList";

    
    protected string GetTextStartPeriod()
    {
        return DateTime.Now.ToShortDateString();
    }

    #region Свойства

    //private Func<ContractSurfaceItemList> _GetSurfaceItemList;
    //public Func<ContractSurfaceItemList> GetSurfaceItemList
    //{
    //    get { return _GetSurfaceItemList; }
    //    set { _GetSurfaceItemList = value; }
    //}

    ///// <summary>
    ///// список поверхностей (контракт, бронь)
    ///// </summary>    
    //public ContractSurfaceItemList SurfaceItemList
    //{
    //    get
    //    {
    //        if (null != GetSurfaceItemList)
    //            return GetSurfaceItemList();

    //        return null;
    //    }
    //}


    /// <summary>
    /// Список в режиме редатирования?
    /// </summary>
    public bool ShowOnlyFilterRows
    {
        get { return true ; }
        set
        {
            ;
        }
    }
    /// <summary>
    /// Префикс 
    /// </summary>    

    private DateTime _StartPeriod;
    public DateTime StartPeriod { get { return _StartPeriod; } set { _StartPeriod = value; } }

    private DateTime _EndPeriod;
    public DateTime EndPeriod { get { return _EndPeriod; } set { _EndPeriod = value; } }

    private DateTimeTools.TimePeriod _PeriodType = DateTimeTools.TimePeriod.Month;
    public DateTimeTools.TimePeriod PeriodType { get { return _PeriodType; } set { _PeriodType = value; } }

    public string Lexeme
    {
        get { return Where.ListLoadStatment.Lexeme; }
        set { Where.ListLoadStatment.Lexeme = value; }
    }


    #endregion

    #region Список
    public void MyDataBind()
    {

        ObjectDS_PF ODS = new ObjectDS_PF(GetList);
        lvSides.DataSource = ODS.ImAsObjectDataSource;
        lvSides.DataBind();
        pnlHeader.DataBind();
    }

    public ListWhere Where
    {
        get
        {
            string key = "Where" + KeyPref;
            if (!CacheTools.IsSessionCacheObject(key))
            {
                ListWhere r = new ListWhere();
                r.ListLoadStatment = new SideListWithContracts.SqlLoadStatment();

                r.SortCondition.SortDirection = SortDirection.Ascending;
                r.SortCondition.SortField = "FullGID";
                CacheTools.SetSessionCacheObject(key, r);
            }
            return (ListWhere)CacheTools.GetSessionCacheObect(key);
        }
        set 
        {

            string key = "Where" + KeyPref;
            CacheTools.SetSessionCacheObject(key, value);

        }

    }

    grdSurface[] rs = null;
    protected grdSurface[] GetList(int startRowIndex, int maximumRows, out int RowCount)
    {
        RowCount = 0;
        return null;
        //if (rs != null)
        //{
        //    RowCount = rs.Length;
        //    return rs;
        //}

        //ws_Constractions ws = new ws_Constractions();
        //SideListWithContracts.SurfaceItemBox[] r = ws.GetSideListWithContracts((SideListWithContracts.SqlLoadStatment)Where.ListLoadStatment);

        //RowCount = r.Length;
        //rs = new grdSurface[r.Length];
        //int j = 0;
        //foreach (SideListWithContracts.SurfaceItemBox i in r)
        //{
        //    rs[j] = new grdSurface(i);
        //    j++;
        //}
        //RowCount = rs.Length;

        //////////заряжаем comparer функцией сортировки         
        //Where.SortCondition.fnOrderBy = SortFunctions.fOrderBy;
        //Array.Sort(rs, Where.SortCondition);
        //return rs;


    }

    protected ListWhere GetWhere()
    {
        return Where;
    }


    #endregion
        
    

    protected ContractSurfaceItem GetItemFromSurfaceItemList(string SurfaceID)
    {
        {
            return null;
            //if (null == SurfaceItemList[SurfaceID]) return null;
            //return (ContractSurfaceItem)SurfaceItemList[SurfaceID];
        }
    }


    protected bool IsSelectedRecord(string ID)
    {
        return false;
        //if (null == SurfaceItemList) return false;
        //return null != SurfaceItemList[ID];
    }

    protected void chSel_CheckedChanged(object sender, EventArgs e)
    {
        //string id = ((CheckBox)(sender)).CssClass;
        //if (null != SurfaceItemList[id]) SurfaceItemList.Remove(id);
        //else SurfaceItemList.Add(new ContractSurfaceItem() { ID = id });

    }

    protected void lvSides_PagePropertiesChanged(object sender, EventArgs e)
    {
        btnFind_Click(null, null);
    }

    protected string GetHeadCss(string HearTd)
    {
        if (Where.SortCondition.SortField != HearTd) return string.Empty;
        return "sort" + Where.SortCondition.SortDirection.ToString();
    }

    protected void lvSort(object sender, EventArgs e)
    {
        lvSides.EditIndex = -1;
        string SortField = ((LinkButton)sender).CommandArgument;
        Where.SortCondition.SortDirection = (Where.SortCondition.SortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending); ;
        Where.SortCondition.SortField = SortField;
        btnFind_Click(null, null);
    }

    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            DateTimeTools.TimePeriod p = ConvertIntToPeriodType(cmbTermCount.SelectedIndex);
            DateTime _st = Convert.ToDateTime(txtStart.Text);
            DateTime _fn = _st;
            int add = Convert.ToInt16(cmbAddPeriod.SelectedValue);
            switch (p)
            {
                case DateTimeTools.TimePeriod.Month:
                    _fn = _st.AddMonths(add);
                    break;
                case DateTimeTools.TimePeriod.Day:
                    _fn = _st.AddDays(add);
                    break;
                case DateTimeTools.TimePeriod.Week:
                    _st = DateTimeTools.GetFisrtDayInWeek(_st);
                    _fn = _st.AddDays(add * 7);
                    break;
            }

            StartPeriod = _st;
            EndPeriod = _fn;
            //Lexeme = txtLexeme.Text;
            ShowOnlyFilterRows = chShowOnlySelected.Checked;
            PeriodType = p;
            MyDataBind();
        }
        catch
        {
        }

    }

    protected void lvSides_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private int ConvertPeriodTypeToInt()
    {
        switch (PeriodType)
        {
            case DateTimeTools.TimePeriod.Month:
                return 0;
            case DateTimeTools.TimePeriod.Week:
                return 1;
            case DateTimeTools.TimePeriod.Day:
                return 2;
        }
        return -1;
    }

    private DateTimeTools.TimePeriod ConvertIntToPeriodType(int i)
    {
        switch (i)
        {
            case 0:
                return DateTimeTools.TimePeriod.Month;
            case 1:
                return DateTimeTools.TimePeriod.Week;
            case 2:
                return DateTimeTools.TimePeriod.Day;
        }
        return DateTimeTools.TimePeriod.Month;
    }

    

    








    protected void Button2_Click(object sender, EventArgs e)
    {
        string  S=Where.ToXml(true);
        ListWhere w = (ListWhere)ReflectionItem.CreateInstance(S);
        w.ReadXml(S);
        string s=w.ListLoadStatment.Lexeme;

        foreach (Filter f in GlobalConstants.CurrentEmployeer.FilterList)
        {
            string ggg= f.Value.ListLoadStatment.Lexeme;
        }


    }
}

